package com.ruoyi.web.controller.fenxi;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.CacheUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ROrg;
import com.ruoyi.system.domain.ROrgRiskEvent;
import com.ruoyi.system.service.IROrgRiskEventService;
import com.ruoyi.system.service.IROrgService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 机构列Controller
 *
 * @author ruoyi
 * @date 2022-09-25
 */
@Controller
@RequestMapping("/system/org")
public class ROrgController extends BaseController
{
    private String prefix = "system/org";

    @Autowired
    private IROrgService rOrgService;

    @Autowired
    private IROrgRiskEventService rOrgRiskEventService;

    @Autowired
    private ISysDictTypeService dictTypeService;

//    @RequiresPermissions("system:org:view")
    @GetMapping()
    public String org()
    {
        return prefix + "/org";
    }

    /**
     * 查询机构列列表
     */
//    @RequiresPermissions("system:org:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ROrg rOrg)
    {
        startPage();
        List<ROrg> list = rOrgService.selectROrgList(rOrg);
        return getDataTable(list);
    }

    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo  Getlist(ROrg rOrg)
    {
//        startPage(1,20);
//        List<ROrg> list = (List<ROrg>) CacheUtils.get(CacheUtils.ORG_KEY);
//        if(Objects.isNull(list)||list.isEmpty())   {
//            list =  rOrgService.selectROrgList(rOrg);
//            CacheUtils.put(CacheUtils.ORG_KEY,list);
//        }
        startPage();
        List<ROrg> list = rOrgService.selectROrgList(rOrg);
        return getDataTable(list);
    }


    @GetMapping("/one")
    @ResponseBody
    public AjaxResult  Getone(ROrg rOrg)
    {
        Map<String,Object> map = new HashMap<>();
        ROrg ROrg = new ROrg();
        SysUser sysUser = getSysUser();
        String deptName = sysUser.getDept().getDeptName();
        List<ROrg> list = rOrgService.selectROrgList(rOrg);
        if(!list.isEmpty()){
            ROrg = list.get(0);
            Long orgId = 0L;
            for (ROrg org : list) {
                if(deptName.equals(org.getName())){
                    ROrg = org;
                    orgId = org.getId();
                }
                ROrgRiskEvent ROrgRiskEvent = new ROrgRiskEvent();
                if(!orgId.equals(0L)){
                    ROrgRiskEvent.setOrgId(orgId);
                }
                ROrgRiskEvent.setOrgId(Objects.isNull(rOrg.getId())?1L:rOrg.getId());
                startPage(1,20);
                List<com.ruoyi.system.domain.ROrgRiskEvent> rOrgRiskEvents = rOrgRiskEventService.selectROrgRiskEventList(ROrgRiskEvent);
                if(StringUtils.isNotBlank(rOrg.getSort())&&rOrg.getSort().equals("desc")){
                    Collections.reverse(rOrgRiskEvents);
                }
                map.put("rOrgRiskEvents",rOrgRiskEvents);
            }
        }
        map.put("ROrg",ROrg);
        return new AjaxResult().success("成功",map);
    }
    /**
     * 导出机构列列表
     */
//    @RequiresPermissions("system:org:export")
    @Log(title = "机构列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ROrg rOrg)
    {
        List<ROrg> list = rOrgService.selectROrgList(rOrg);
        ExcelUtil<ROrg> util = new ExcelUtil<ROrg>(ROrg.class);
        return util.exportExcel(list, "机构列数据");
    }

    /**
     * 新增机构列
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存机构列
     */
//    @RequiresPermissions("system:org:add")
    @Log(title = "机构列", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ROrg rOrg)
    {
        return toAjax(rOrgService.insertROrg(rOrg));
    }

    /**
     * 修改机构列
     */
//    @RequiresPermissions("system:org:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ROrg rOrg = rOrgService.selectROrgById(id);
        mmap.put("rOrg", rOrg);
        return prefix + "/edit";
    }

    /**
     * 修改保存机构列
     */
//    @RequiresPermissions("system:org:edit")
    @Log(title = "机构列", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ROrg rOrg)
    {
        return toAjax(rOrgService.updateROrg(rOrg));
    }

    /**
     * 删除机构列
     */
//    @RequiresPermissions("system:org:remove")
    @Log(title = "机构列", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rOrgService.deleteROrgByIds(ids));
    }
}
